package com.jmx.yp.service.member;

import java.util.*;

import com.jmx.yp.dao.IBaseDao;
import com.jmx.yp.entity.EmpCategory;
import com.jmx.yp.exception.*;
import com.jmx.yp.util.Tools;

/**
 * 
 * 功能描述：
 *        ICatetoryService的实现类
 * @author Jenny
 *
 * Date:2011-2-15 下午04:17:47
 */
public class CategoryService implements ICategoryService {
	
	private IBaseDao dao;
	
	public void setDao(IBaseDao dao){
		this.dao = dao;
	}
	
	/**
	 *  新增会员类别
	 * @param category
	 * @throws Exception 当会员名称相同时，抛出HandlerException
	 */
	public void addCategory(EmpCategory category) throws Exception{
		if(isSameName(category.getName(), null))
			throw new HandlerException(ErrorCode.ERR_4000);
		
		dao.saveObject("com.jmx.entity.EmpCategoryMapper.addEmpCategory", category);
	}
	
	/**
	 * 是否相同级别名称
	 * @param name
	 * @param serialid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private boolean isSameName(String name, Long serialid){
		boolean flag = false;
		
		Map pMap = new HashMap();
		pMap.put("categoryName", name.trim());
		if(serialid != null)
			pMap.put("cid", serialid);
		
		int count = (Integer)dao.getObject("com.jmx.yp.entity.EmpCategoryMapper.isSameName", pMap);
		if(count > 0)
			flag = true;
		
		return flag;
	}
	
	/**
	 * 根据名称查询类别
	 * @param queryName
	 * @param nowPage
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public Map queryCategory(String queryName, int nowPage) throws Exception{
		Map map = new HashMap(2);
		int pageSize = dao.getPageSize();
		
		//参数MAP
		Map paramMap = new HashMap();
		if(queryName != null && queryName.trim().length() > 0)
			paramMap.put("queryName", "%" + queryName.trim() + "%");
		
		//总数
		int total = (Integer)dao.getObject("com.jmx.yp.entity.EmpCategoryMapper.countQueryCategory", paramMap);
		
		paramMap.put("pageSize", pageSize);
		paramMap.put("page", (nowPage - 1) * pageSize);
		
		//LIST
		List list = dao.getList("com.jmx.yp.entity.EmpCategoryMapper.queryCategory", paramMap);
		if(nowPage > 1 && (list == null || list.isEmpty())){ //当前页>1，查询又无结果，说明是以前的页数已经被删完。则自动显示最后一页
			nowPage = Tools.computeTotalPage(total, pageSize);
			paramMap.put("page", (nowPage - 1) * pageSize);
			list = dao.getList("com.jmx.yp.entity.EmpCategoryMapper.queryCategory", paramMap);
		}
		
		//handleListTel(list);
		
		map.put("total", total); //总记录数
		map.put("rows", list);
		map.put("nowPage", nowPage); //当前页
		map.put("pageTotal", Tools.computeTotalPage(total, pageSize));                    //总页数
		
		return map;
	}
	
	/**
	 * 根据ID查询会员类别
	 * @param serialid
	 * @return
	 * @throws Exception
	 */
	public EmpCategory getEmpCategory(Long serialid) throws Exception{
		EmpCategory category = (EmpCategory)dao.getObject("com.jmx.yp.entity.EmpCategoryMapper.getCategory", serialid);
		return category;
	}
	
	
	/**
	 * 修改会员类别
	 * @param category
	 * @throws Exception
	 */
	public void updateCategory(EmpCategory category) throws Exception{
		if(isSameName(category.getName(), category.getSerialid()))
			throw new HandlerException(ErrorCode.ERR_4000);
		
		dao.updateObject("com.jmx.yp.entity.EmpCategoryMapper.updateEmpCategory", category);
	}
	
	/**
	 * 删除会员类别（支持批量删除）
	 * @param serialid
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public void deleteCategory(String serialid, String operator) throws Exception{
		if(isRelationGrade(serialid))
			throw new HandlerException(ErrorCode.ERR_4001);
		
		Map map = new HashMap(3);
		map.put("array", serialid.split(";"));
		map.put("operator", operator);
		map.put("operateTime", new Date());
		
		dao.updateObject("com.jmx.yp.entity.EmpCategoryMapper.deleteCategory", map);
	}
	
	/**
	 * 是否关联会员级别
	 * @param serialid
	 * @return
	 * @throws Exception
	 */
	private boolean isRelationGrade(String serialid) throws Exception{
		boolean flag = false;
		
		int count = (Integer)dao.getObject("com.jmx.yp.entity.EmpCategoryMapper.isRelationGrade", serialid.split(";"));
		if(count > 0)
			flag = true;
		
		return flag;
	}

	/**
	 * 根据类别ID查询关联的会员级别
	 * @param categoryId
	 * @return
	 * @throws Exception
	 */
	public List queryGradeByCategoryId(Long categoryId) throws Exception{
		List gradeList = dao.getList("com.jmx.yp.entity.EmpCategoryMapper.queryGradeByCategoryId", categoryId);
		return gradeList;
	}
	
	/**
	 * 根据名称查询类别
	 * @param queryName
	 * @return 返回包含MAP的LIST
	 * @throws Exception
	 */
	public List queryCategory(String queryName) throws Exception{
		Map map = new HashMap();
		if(queryName != null && queryName.trim().length() > 0){
			map.put("queryName", "%" + queryName.trim() + "%");
		}
		
		List list = dao.getList("com.jmx.yp.entity.EmpCategoryMapper.queryCategory", map);
		
		return list;
	}
}
